package com.lrr.community.service;

import com.lrr.community.dto.PaginationDTO;
import com.lrr.community.dto.QuestionDto;
import com.lrr.community.mapper.QuestionMapper;
import com.lrr.community.mapper.UserMapper;
import com.lrr.community.model.Question;
import com.lrr.community.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

// TODO 在这里既可以使用QuestionMap，
//  也可以使用UserMap 起到组装的作用
@Service
public class QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;

    public List<QuestionDto> list() {
        List<Question> questions = questionMapper.list1();
        List<QuestionDto> questionDtos = new LinkedList<>();
        for (Question question:questions){
            User user = userMapper.findById(question.getCreateor());
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            questionDtos.add(questionDto);
        }
        return questionDtos;
    }

    public PaginationDTO list(Integer page, Integer size) {
        Integer totalPage;
        Integer totalCount = questionMapper.count(); // 所有记录

        if(totalCount%size==0){
            totalPage = totalCount / size;
        }
        else{
            totalPage = totalCount / size + 1;
        }
        if(page<1)
            page=1;
        if(page>totalPage)
            page = totalPage;
        // size * (page - 1)
        Integer offset = size * (page-1);
        List<Question> questions = questionMapper.list(offset,size);
        List<QuestionDto> questionDtos = new LinkedList<>();
        PaginationDTO paginationDTO = new PaginationDTO();
        paginationDTO.setTotalPage(totalPage);
        paginationDTO.setPagination(totalCount,page,size);
        for (Question question:questions){
            User user = userMapper.findById(question.getCreateor());
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            questionDtos.add(questionDto);
        }
        paginationDTO.setQuestions(questionDtos);

        return paginationDTO;
    }

    public PaginationDTO list(int userId, Integer page, Integer size) {
        Integer totalPage;
        Integer totalCount = questionMapper.countById(userId); // 所有记录

        if(totalCount%size==0){
            totalPage = totalCount / size;
        }
        else{
            totalPage = totalCount / size + 1;
        }
        if(page<1)
            page=1;
        if(page>totalPage)
            page = totalPage;
        // size * (page - 1)
        Integer offset = size * (page-1);
        List<Question> questions = questionMapper.listById(userId,offset,size);
        List<QuestionDto> questionDtos = new LinkedList<>();
        PaginationDTO paginationDTO = new PaginationDTO();
        paginationDTO.setTotalPage(totalPage);
        paginationDTO.setPagination(totalCount,page,size);
        for (Question question:questions){
            User user = userMapper.findById(question.getCreateor());
            QuestionDto questionDto = new QuestionDto();
            BeanUtils.copyProperties(question,questionDto);
            questionDto.setUser(user);
            questionDtos.add(questionDto);
        }
        paginationDTO.setQuestions(questionDtos);
        return paginationDTO;
    }

    public QuestionDto getById(Integer id) {
        Question question = questionMapper.getById(id);
        QuestionDto questionDto = new QuestionDto();
        BeanUtils.copyProperties(question,questionDto);
        User user = userMapper.findById(question.getCreateor());
        questionDto.setUser(user);
        return questionDto;
    }

    public void createOrUpdate(Question question) {
        if(question.getId()==null){
            questionMapper.create(question);
        }
        else{
            question.setGmt_modified(System.currentTimeMillis());
            questionMapper.update(question);
        }
    }
}
